//
//  STLoginViewController.m
//  shitong
//
//  Created by yyx on 15/5/31.
//  Copyright (c) 2015年 shitong. All rights reserved.
//

#import "STLoginViewController.h"
#import "UIView+MJ.h"
#import "UIImage+MJ.h"
#import "STSettingViewController.h"

#define STMargin 30
// 获得RGB颜色
#define STColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

@interface STLoginViewController ()<UITextFieldDelegate>

/**
 *  理财室View
 */
@property (nonatomic , weak) UIView *homeView;
/**
 *  错误信息提示
 */
@property (nonatomic , weak) UILabel *messageLabel;
@end

@implementation STLoginViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"登录";

    NSLog(@"self.navigationController.navigationBar:%@ self.view:%@",self.navigationController.navigationBar, self.view);
    
    // 创建UI控件
    [self setUpSubview];
    
}

- (void)setUpSubview
{

    // 设置
    CGFloat settingBtnW = 100;
    CGFloat settingBtnH = 50;
    CGFloat settingBtnX = 900;
    CGFloat settingBtnY = self.navigationController.navigationBar.height + STMargin;
    UIButton *settingBtn = [[UIButton alloc] initWithFrame:CGRectMake(settingBtnX, settingBtnY, settingBtnW, settingBtnH)];
    settingBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [settingBtn setTitle:NSLocalizedString(@"设置", @"设置") forState:UIControlStateNormal];
    settingBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [settingBtn setTitleColor:[UIColor colorWithRed:32 / 255.0 green:134 / 255.0 blue:158 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    [settingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [settingBtn addTarget:self action:@selector(settingBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:settingBtn];

    // 员工编号
    CGFloat numberLabelX = 250;
    CGFloat numberLabelY = CGRectGetMaxY(settingBtn.frame) + 100;
    CGFloat numberLabelW = 100;
    CGFloat numberLabelH = 50;
    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(numberLabelX, numberLabelY, numberLabelW, numberLabelH)];
    numberLabel.font = [UIFont systemFontOfSize:20];
    numberLabel.backgroundColor = [UIColor clearColor];
    numberLabel.textColor = [UIColor blackColor];
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.text = @"员工编号";
    [self.view addSubview:numberLabel];
    
    // 员工编号输入框
    CGFloat numberTextFieldX = CGRectGetMaxX(numberLabel.frame) + STMargin;
    CGFloat numberTextFieldY = numberLabelY;
    CGFloat numberTextFieldW = 300;
    CGFloat numberTextFieldH = numberLabelH;
    UITextField *numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(numberTextFieldX, numberTextFieldY, numberTextFieldW, numberTextFieldH)];
    numberTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
    numberTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    numberTextField.leftViewMode = UITextFieldViewModeAlways;
    numberTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    numberTextField.font = [UIFont systemFontOfSize:18.0];
    numberTextField.backgroundColor = [UIColor whiteColor];
    numberTextField.textColor = [UIColor blackColor];
    numberTextField.placeholder = NSLocalizedString(@"请输入员工编号", @"请输入员工编号");
    numberTextField.returnKeyType = UIReturnKeyDone;
    numberTextField.delegate = self;
    numberTextField.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:numberTextField];
    
    // 密码
    CGFloat pwdLabelX = numberLabelX;
    CGFloat pwdLabelY = CGRectGetMaxY(numberLabel.frame) + STMargin;
    CGFloat pwdLabelW = numberLabelW;
    CGFloat pwdLabelH = numberLabelH;
    UILabel *pwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(pwdLabelX, pwdLabelY, pwdLabelW, pwdLabelH)];
    pwdLabel.font = [UIFont systemFontOfSize:20];
    pwdLabel.backgroundColor = [UIColor clearColor];
    pwdLabel.textColor = [UIColor blackColor];
    pwdLabel.textAlignment = NSTextAlignmentCenter;
    pwdLabel.text = @"密码";
    [self.view addSubview:pwdLabel];
    
    // 密码输入框
    CGFloat pwdTextFieldX = numberTextFieldX;
    CGFloat pwdTextFieldY = pwdLabelY;
    CGFloat pwdTextFieldW = numberTextFieldW;
    CGFloat pwdTextFieldH = numberTextFieldH;
    UITextField *pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(pwdTextFieldX, pwdTextFieldY, pwdTextFieldW, pwdTextFieldH)];
    pwdTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
    pwdTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    pwdTextField.leftViewMode = UITextFieldViewModeAlways;
    pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    pwdTextField.font = [UIFont systemFontOfSize:18.0];
    pwdTextField.backgroundColor = [UIColor whiteColor];
    pwdTextField.textColor = [UIColor blackColor];
    pwdTextField.placeholder = NSLocalizedString(@"请输入密码", @"请输入密码");
    pwdTextField.returnKeyType = UIReturnKeyDone;
    pwdTextField.delegate = self;
    pwdTextField.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:pwdTextField];
    
    // 5个按钮
    NSArray *homeArray = [NSArray arrayWithObjects:@"理财室1", @"理财室2", @"理财室3", @"理财室4", @"分行",nil];
    
    CGFloat segBtnH = 50;
    CGFloat segBtnW = 100;
    
    UIView *homeView = [[UIView alloc] init];
    homeView.backgroundColor = [UIColor clearColor];
    homeView.frame = CGRectMake(pwdLabelX, CGRectGetMaxY(pwdLabel.frame) + STMargin, segBtnW * 5 + STMargin * 4, segBtnH);
    [self.view addSubview:homeView];
    self.homeView = homeView;
    
    for (int i = 0; i < homeArray.count; i++) {
        
        UIButton *homeBtn = [[UIButton alloc] init];
        
        homeBtn.tag = 500 + i;
        [homeBtn setTitle:[homeArray objectAtIndex:i] forState:UIControlStateNormal];
        [homeBtn setTitle:[homeArray objectAtIndex:i] forState:UIControlStateHighlighted];
        [homeBtn setTitleColor:STColor(58, 58, 58) forState:UIControlStateNormal];
        [homeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [homeBtn setBackgroundImage:[UIImage imageWithColor:STColor(224, 243, 255)] forState:UIControlStateSelected];
        homeBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [homeBtn.layer setMasksToBounds:YES];
        [homeBtn.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
        [homeBtn.layer setBorderWidth:1.0]; //边框宽度
        [homeBtn.layer setBorderColor:(__bridge CGColorRef)([UIColor blackColor])];//边框颜色
        [homeBtn addTarget:self action:@selector(homeBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat segBtnX = i * (segBtnW + STMargin);
        CGFloat segBtnY = 0;
        homeBtn.frame = CGRectMake(segBtnX, segBtnY, segBtnW, segBtnH);
        [homeView addSubview:homeBtn];
     
    }
    
    // 登录
    UIButton *loginBtn = [[UIButton alloc] init];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitle:@"登录" forState:UIControlStateHighlighted];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    loginBtn.backgroundColor = [UIColor blueColor];
    loginBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [loginBtn.layer setMasksToBounds:YES];
    [loginBtn.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
    [loginBtn.layer setBorderWidth:1.0]; //边框宽度
    [loginBtn.layer setBorderColor:(__bridge CGColorRef)([UIColor blackColor])];//边框颜色
    [loginBtn addTarget:self action:@selector(loginBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    CGFloat loginBtnX = pwdLabelX;
    CGFloat loginBtnY =CGRectGetMaxY(homeView.frame)+STMargin;
    loginBtn.frame = CGRectMake(loginBtnX, loginBtnY, 500, 50);
    [self.view addSubview:loginBtn];
    
    // 登录错误信息提示
    CGFloat messageLabelX = pwdLabelX;
    CGFloat messageLabelY = CGRectGetMaxY(loginBtn.frame) + 20;
    CGFloat messageLabelW = 500;
    CGFloat messageLabelH = 50;
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(messageLabelX, messageLabelY, messageLabelW, messageLabelH)];
    messageLabel.font = [UIFont systemFontOfSize:20];
    messageLabel.backgroundColor = [UIColor clearColor];
    messageLabel.textColor = [UIColor blackColor];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.text = @"错误信息提示";
    [self.view addSubview:messageLabel];
    self.messageLabel = messageLabel;
    
}

/**
 *  设置
 */
- (void)settingBtnDidClick
{
    STSettingViewController *stVc = [[STSettingViewController alloc] initWithNibName:@"STSettingViewController" bundle:nil];
    [self.navigationController pushViewController:stVc animated:YES];
    
}

/**
 *  理财室选择
 *
 *  @param btn btn
 */
- (void)homeBtnDidClick:(UIButton *)btn
{
    for (UIView *subView in self.homeView.subviews) {
        
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *button=(UIButton*)subView;
            if (button.tag==btn.tag) {
                button.selected=YES;
            }
            else
            {
                button.selected=NO;
            }
        }
    }

    
}

/**
 *  登录
 */
- (void)loginBtnDidClick
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
